package com.nowcoder.community.controller.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class DemoInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(DemoInterceptor.class);

    @Override
    //在controller之前执行，如果返回false，程序就结束了，后面的controller不会被执行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("preHandle："+handler.toString());
        return true;
    }

    @Override
    //在controller之后执行
    //有个参数是ModelAndView，因为是在controller之后执行，请求逻辑已经写完，后面是渲染页面了
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.debug("postHandle："+handler.toString());
    }

    @Override
    //在程序的最后执行，在模板引擎之后执行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.debug("afterCompletion："+handler.toString());
    }
}
